package com.zengdada.dml.utils;

import javax.swing.table.DefaultTableModel;
import java.util.*;

/**
 * dml执行后返回对象
 */
public class DmlExecuteReturn {

    private int dmlType;
    private final DefaultTableModel data;
    private int rowsUpdated = -1;
    private int totalRow;

    public DmlExecuteReturn() {
        this.totalRow = -1;
        this.data = new DefaultTableModel();
    }

    public int getDmlType() {
        return dmlType;
    }

    public void setDmlType(int dmlType) {
        this.dmlType = dmlType;
    }

    public int getRowsUpdated() {
        return rowsUpdated;
    }

    public void setRowsUpdated(int rowsUpdated) {
        this.rowsUpdated = rowsUpdated;
    }

    public DefaultTableModel getData() {
        return data;
    }

    public int getTotalRow() {
        return totalRow == -1 ? this.data.getRowCount() : totalRow;
    }

    public void setTotalRow(int totalRow) {
        this.totalRow = totalRow;
    }

    public List<Map<String, Object>> getDataMap() {
        DefaultTableModel data = this.getData();
        if (data == null) {
            return null;
        }
        int columnCount = data.getColumnCount(),
                rowCount = data.getRowCount();
        List<Map<String, Object>> rows = new ArrayList<>(rowCount);
        for (int ri = 0; ri < rowCount; ri++) {
            Map<String, Object> row = new LinkedHashMap<>();
            for (int ci = 0; ci < columnCount; ci++) {
                row.put(data.getColumnName(ci), data.getValueAt(ri, ci));
            }
            rows.add(row);
        }
        return rows;
    }
}
